package com.imooc.article.service.impl;

import com.github.pagehelper.PageHelper;
import com.imooc.api.service.BaseService;
import com.imooc.article.mapper.ArticleMapper;
import com.imooc.article.service.ArticlePortalService;
import com.imooc.enums.ArticleReviewStatus;
import com.imooc.enums.YesOrNo;
import com.imooc.exception.GraceException;
import com.imooc.grance.result.ResponseStatusEnum;
import com.imooc.pojo.Article;
import com.imooc.pojo.Category;
import com.imooc.pojo.vo.ArticleDetaiVO;
import com.imooc.utils.IPUtil;
import com.imooc.utils.JsonUtils;
import com.imooc.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class ArticlePortalServiceImpl extends BaseService implements ArticlePortalService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public PagedGridResult articleProtalList(String keyword, Integer page, Integer pageSize, Integer category) {
        Example example = new Example(Article.class);
        Example.Criteria criteria = getDefaultCriteria(example);
        if (StringUtils.isNotBlank(keyword)) {
            criteria.andLike("title", "%"+keyword+"%");
        }
        String redisCats = redis.get(REDIS_ALL_CATEGORY);
        List<Category> categoryList = JsonUtils.jsonToList(redisCats, Category.class);
        if (categoryList != null && category != null) {
            for (Category c : categoryList) {
                if (category.equals(c.getId())) {
                    criteria.andEqualTo("categoryId", category);
                    break;
                }
            }
        }

        PageHelper.startPage(page, pageSize);
        List<Article> articles = articleMapper.selectByExample(example);
        PagedGridResult pagedGridResult = pagedResultSettings(articles, page);
        return pagedGridResult;
    }


    @Override
    public List<Article> getHotList() {
        Example example = new Example(Article.class);
        Example.Criteria criteria = getDefaultCriteria(example);
        PageHelper.startPage(COMMON_PAGE, COMMON_PAGESIZE);
        List<Article> list = articleMapper.selectByExample(example);

        return list;
    }

    private Example.Criteria getDefaultCriteria(Example example) {

        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isAppoint", YesOrNo.NO.type);
        criteria.andEqualTo("isDelete", YesOrNo.NO.type);
        criteria.andEqualTo("articleStatus", ArticleReviewStatus.SUCCESS.type);
        example.orderBy("publishTime").desc();
        return criteria;
    }

    @Override
    public PagedGridResult writerArticleList(String writerId,
                                             Integer page,
                                             Integer pageSize) {
        Example example = new Example(Article.class);
        Example.Criteria criteria = getDefaultCriteria(example);
        criteria.andEqualTo("publishUserId", writerId);
        PageHelper.startPage(page, pageSize);
        List<Article> list = articleMapper.selectByExample(example);
        PagedGridResult pagedGridResult = pagedResultSettings(list, page);
        return pagedGridResult;
    }
    @Override
    public PagedGridResult writerHotArticleList(String writerId) {
        Example example = new Example(Article.class);
        Example.Criteria criteria = getDefaultCriteria(example);
        criteria.andEqualTo("publishUserId", writerId);
        PageHelper.startPage(COMMON_PAGE, COMMON_PAGESIZE);
        List<Article> list = articleMapper.selectByExample(example);
        PagedGridResult pagedGridResult = pagedResultSettings(list, COMMON_PAGE);
        return pagedGridResult;
    }

    @Override
    public ArticleDetaiVO getArticleDetaiVO(String articleId) {
        Article article = new Article();
        article.setId(articleId);
        article.setIsAppoint(YesOrNo.NO.type);
        article.setIsAppoint(YesOrNo.NO.type);
        article.setIsDelete(YesOrNo.NO.type);
        article.setArticleStatus(ArticleReviewStatus.SUCCESS.type);
        Article newArticle = articleMapper.selectOne(article);
//        System.out.println("cover: "+newArticle.getArticleCover());
        ArticleDetaiVO articleDetaiVO = new ArticleDetaiVO();
        String readCounts = getCountsFromRedis(REDIS_ARTICLE_READ_COUNTS + ":" + articleId);
        articleDetaiVO.setReadCounts(readCounts);
        articleDetaiVO.setCover(newArticle.getArticleCover());
        articleDetaiVO.setArticleId(newArticle.getId());
        BeanUtils.copyProperties(newArticle, articleDetaiVO);
        return articleDetaiVO;
    }

    @Override
    public void readArticle(String articleId, HttpServletRequest request) {
        String ip = IPUtil.getRequestIp(request);
        //在redis中新建一个永久的key，新建一个拦截器进行拦截，如果key存在则不进入如此该方法，该篇文章的阅读数就不会增加
        redis.setnx(REDIS_ALREADY_READ+":"+articleId+":"+ip,ip);
        redis.increment(REDIS_ARTICLE_READ_COUNTS + ":" + articleId,1);
    }



}
